package com.mw.mwreaderdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class SLE42_28 extends Activity {

	private Button btnReadPwdErrCounter;
	private Button btnChkPwd;
	private Button btnChangePwd;
	private Button btnReadPwd;
	private Button btnReadData;
	private Button btnWriteData;
	private RadioButton radioButton42Card;
	private RadioButton radioButton28Card;
	private EditText etPwd;
	private EditText etData;
	private EditText etOffset;
	private EditText etLen;
	private EditText etResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*
		 * if (this.getResources().getConfiguration().orientation ==
		 * Configuration.ORIENTATION_LANDSCAPE) { System.out.println("横屏"); }
		 * else if (this.getResources().getConfiguration().orientation ==
		 * Configuration.ORIENTATION_PORTRAIT) { System.out.println("竖屏"); }
		 */
		setContentView(R.layout.activity_sle42_28);

		btnReadPwdErrCounter = (Button) findViewById(R.id.btnReadPwdErrCounter);
		btnChkPwd = (Button) findViewById(R.id.btnChkPwd);
		btnChangePwd = (Button) findViewById(R.id.btnChangePwd);
		btnReadPwd = (Button) findViewById(R.id.btnReadPwd);
		btnReadData = (Button) findViewById(R.id.btnReadData);
		btnWriteData = (Button) findViewById(R.id.btnWriteData);
		radioButton42Card = (RadioButton) findViewById(R.id.radioButton42Card);
		radioButton28Card = (RadioButton) findViewById(R.id.radioButton28Card);
		etPwd = (EditText) findViewById(R.id.etPwd);
		etData = (EditText) findViewById(R.id.etData);
		etOffset = (EditText) findViewById(R.id.etOffset);
		etLen = (EditText) findViewById(R.id.etLen);
		etResult = (EditText) findViewById(R.id.result);

		// 将输入字符转换为大写
		etData.setTransformationMethod(new InputLowerToUpper());
		etPwd.setTransformationMethod(new InputLowerToUpper());

		radioButton42Card.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isCheck) {
				// TODO Auto-generated method stub
				if (isCheck) {
					etPwd.setFilters(new InputFilter[] { new InputFilter.LengthFilter(6) });
					etPwd.setText("FFFFFF");
				} else {
					etPwd.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) });
					etPwd.setText("FFFF");
				}
			}
		});
		radioButton42Card.setChecked(true);

		etData.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable ed) {
				// TODO Auto-generated method stub
				etLen.setText(ed.toString().length() + "");
			}
		});

		btnReadPwdErrCounter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(SLE42_28.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					if (radioButton42Card.isChecked()) {
						etResultAddStr("卡片密码错误计数器的值 为: " + MainActivity.reader.getErrorCounter4442());
					} else {
						etResultAddStr("卡片密码错误计数器的值 为: " + MainActivity.reader.getErrorCounter4428());
					}
				} catch (Exception e) {
					// TODO: handle exception
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnChkPwd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(SLE42_28.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					if (radioButton42Card.isChecked()) {
						MainActivity.reader.verifyPassword4442(etPwd.getText().toString());
					} else {
						MainActivity.reader.verifyPassword4428(etPwd.getText().toString());
					}
					etResultAddStr("密码验证成功!");
				} catch (Exception e) {
					// TODO: handle exception
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnChangePwd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(SLE42_28.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					String pwd = etPwd.getText().toString();
					if (radioButton42Card.isChecked()) {
						if (pwd.length() != 6) {
							AlertDialog.Builder builder = new AlertDialog.Builder(SLE42_28.this);
							builder.setTitle("错误");
							builder.setMessage("您输入正确长度的密码。");
							builder.create();
							builder.show();
							return;
						}
						MainActivity.reader.changePassword4442(pwd);
					} else {
						if (pwd.length() != 4) {
							AlertDialog.Builder builder = new AlertDialog.Builder(SLE42_28.this);
							builder.setTitle("错误");
							builder.setMessage("您输入正确长度的密码。");
							builder.create();
							builder.show();
							return;
						}
						MainActivity.reader.changePassword4428(pwd);
					}
					etResultAddStr("密码修改成功!");
				} catch (Exception e) {
					// TODO: handle exception
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnReadPwd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String strPwd;
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(SLE42_28.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					if (radioButton42Card.isChecked()) {
						strPwd = MainActivity.reader.readPassword4442();
					} else {
						strPwd = MainActivity.reader.readPassword4428();
					}
					etResultAddStr("卡片密码: " + strPwd);
				} catch (Exception e) {
					// TODO: handle exception
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnReadData.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String strData;
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(SLE42_28.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int offset = Integer.parseInt(etOffset.getText().toString());
					int len = Integer.parseInt(etLen.getText().toString());
					if (radioButton42Card.isChecked()) {
						strData = MainActivity.reader.read4442(offset, len);
					} else {
						strData = MainActivity.reader.read4428(offset, len);
					}
					etResultAddStr(strData);
				} catch (Exception e) {
					// TODO: handle exception
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnWriteData.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(SLE42_28.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int offset = Integer.parseInt(etOffset.getText().toString());
					if (radioButton42Card.isChecked()) {
						MainActivity.reader.write4442(offset, etData.getText().toString());
					} else {
						MainActivity.reader.write4428(offset, etData.getText().toString());
					}
					etResultAddStr("写数据成功!");
				} catch (Exception e) {
					// TODO: handle exception
					etResultAddStr(e.getMessage());
				}
			}
		});
	}

	/**
	 * @Description 提示信息框添加信息
	 * @param str
	 *            要添加的信息
	 * @return void
	 */
	private void etResultAddStr(String str) {
		etResult.setText(etResult.getText() + " " + str + "\n");
		etResult.setSelection(etResult.length());// 调整光标到最后一行
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sle42_28, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
